package com.siemens.industrial.app;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

/**
 * Created by qupen on 2016/9/22.
 */
public class DBHelper {
    private static final String driver = "com.mysql.cj.jdbc.Driver";
    private static final String url = "jdbc:mysql://localhost:3306?useUnicode=true&characterEncoding=utf8&charset=utf8&serverTimezone=GMT%2b8&useSSL=false&allowMultiQueries=true";
    private static final String user = "root";
    private static final String pass = "root";

    private static JdbcTemplate jdbcTemplate = null;

    public static JdbcTemplate getJdbcTemplate() {
        if (jdbcTemplate == null) {
            try {
                DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
                driverManagerDataSource.setDriverClassName(driver);
                driverManagerDataSource.setUrl(url);
                driverManagerDataSource.setUsername(user);
                driverManagerDataSource.setPassword(pass);
                jdbcTemplate = new JdbcTemplate(driverManagerDataSource);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return jdbcTemplate;
    }
}
